from flask import Flask, render_template, request
import logging
app = Flask(__name__)

@app.route('/login', methods=['POST'])
def login():
    user = request.form['username']
    app.logger.info(user)
    return "ok"

@app.route('/hello')
def hello():
    app.logger.info("hello")
    return "world"

from flask import has_request_context
from flask.logging import default_handler

class RequestFormatter(logging.Formatter):
    def format(self, record):
        if has_request_context():
            record.url = request.url
            record.remote_addr = request.remote_addr
        else:
            record.url = None
            record.remote_addr = None
        
        return super().format(record)

formatter = RequestFormatter('[%(asctime)s] %(remote_addr)s requested %(url)s\n'
    '%(levelname)s in %(module)s: %(message)s')

default_handler.setFormatter(formatter)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=True)